﻿using AELTAPLC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OKDebug
{
    public partial class FrmOtherPLC : Form
    {
        public PLC_S7200.PLC_S7200 _plc_s7200 = new PLC_S7200.PLC_S7200();
        public SanLingPLC.SanLingPLC _plc_sanling = new SanLingPLC.SanLingPLC();
        public DevPLC_AELTA _AeltaPLC = new DevPLC_AELTA();

        public FrmOtherPLC()
        {
            InitializeComponent();
        }

        private void btnConectPlc_Click(object sender, EventArgs e)
        {
            if (this.btnConectPlc.Text.Trim().Equals("已连接plc √"))
            {
                return;
            }
            bool sucess = false;
            string result = "";
            if (string.IsNullOrEmpty(this.txtIP.Text.Trim()))
            {
                MessageBox.Show("IP不能为空！");
                this.txtIP.Focus();
                return;
            }

            if (this.rdbs7200.Checked)
            {
                this._plc_s7200.DevName = this.txtIP.Text.Trim();
                this._plc_s7200.Port = this.txtIP.Text.Trim();
                sucess = this._plc_s7200.Init("");
            }
            else if (this.rdbsanling.Checked)
            {
                this._plc_sanling.DevName = this.txtIP.Text.Trim();
                this._plc_sanling.Port = this.txtIP.Text.Trim();
                sucess = this._plc_sanling.Init(ref result);
            }
            else if (this.rdbaleta.Checked)
            {
                this._AeltaPLC.DevName = this.txtIP.Text.Trim();
                string portSTR = this.txtIP.Text.Trim();
                if (portSTR.Contains("."))
                {
                    IPort.ILogicPort port = IOHelper.ObjCreator<IPort.ILogicPort>.Instance.CreateLogicDevice("ILogicPort", ".\\TcpClientPort\\PortTcpClient.dll");
                    if (port.Open(portSTR + ",,,,,.\\TcpClientPort\\PortTcpClient.dll"))
                    {
                        this._AeltaPLC._Port = port;
                        sucess = this._AeltaPLC.Init();
                    }
                }
                else
                {
                    IPort.ILogicPort port = IOHelper.ObjCreator<IPort.ILogicPort>.Instance.CreateLogicDevice("ILogicPort", ".\\SerialPort\\PortSerial.dll");
                    if (port.Open(portSTR + ",9600,7,2,1,.\\SerialPort\\PortSerial.dll"))
                    {
                        this._AeltaPLC._Port = port;
                        sucess = this._AeltaPLC.Init();
                    }
                }
            }

            if (sucess)
            {
                this.btnConectPlc.Text = "已连接plc √";
                this.btnConectPlc.ForeColor = Color.White;
                this.btnConectPlc.BackColor = Color.Green;
                this.btndisconnect.Visible = true;
            }
            else
            {
                this.btnConectPlc.Text = "连接plc ×";
                this.btnConectPlc.ForeColor = Color.White;
                this.btnConectPlc.BackColor = Color.Red;
                MessageBox.Show(result);
            }
        }

        private void btndisconnect_Click(object sender, EventArgs e)
        {
            bool sucess = false;
            if (this.rdbs7200.Checked)
            {

            }
            else if (this.rdbsanling.Checked)
            {

            }
            else if (this.rdbaleta.Checked)
            {
                sucess = this._AeltaPLC.Close(null);
            }

            if (sucess)
            {
                this.btnConectPlc.Text = "连接plc";
                this.btnConectPlc.ForeColor = SystemColors.ControlText;
                this.btnConectPlc.BackColor = SystemColors.Control;
                this.btndisconnect.Visible = false;
            }
            else
            {
                MessageBox.Show("断开连接失败！");
            }
        }

        private void btnReadPlc_Click(object sender, EventArgs e)
        {
            if (this.btnConectPlc.Text.Trim().Equals("连接plc"))
            {
                MessageBox.Show("未连接plc！");
                return;
            }
            string addr = this.txtReadAddr.Text.Trim();
            if (string.IsNullOrEmpty(addr))
            {
                MessageBox.Show("读取位置不能为空！");
                this.txtReadAddr.Focus();
                return;
            }

            ushort[] buf = null;
            float[] fbuf = null;
            try
            {
                ushort flag = 0;
                if (this.rdbs7200.Checked)
                {
                    if (!this.cbxDot.Checked)
                    {
                        buf = _plc_s7200.ReadRegister(0x03, addr, 1);
                    }
                    else
                    {
                        fbuf = _plc_s7200.ReadRegisterF(0x03, addr, 2);
                    }
                }
                else if (this.rdbsanling.Checked)
                {
                    buf = _plc_sanling.ReadRegisterDB(0x03, addr, 1);
                }
                else if (this.rdbaleta.Checked)
                {
                    if (!cbxDot.Checked)
                    {
                        buf = _AeltaPLC.ReadRegister(0x03, "D" + addr, 1);
                    }
                    else
                    {
                        //buf = _AeltaPLC.ReadRegister(0x03, "D" + addr, 2);
                        ushort[] v = _AeltaPLC.ReadRegister(0x03, "D" + addr, 1);
                        ushort[] v2 = _AeltaPLC.ReadRegister(0x03, "D" + (Convert.ToInt32(addr) + 1), 1);

                        if (v == null || v.Length != 1)
                        {
                            MessageBox.Show("读取错误" + (v == null ? " 空" : v.Length.ToString()));
                            return;
                        }

                        if (v2 == null || v2.Length != 1)
                        {
                            MessageBox.Show("读取错误" + (v2 == null ? " 空" : v2.Length.ToString()));
                            return;
                        }

                        buf = new ushort[2] { v[0], v2[0] };
                    }
                }

                if (buf != null && buf.Length > 0)
                {
                    if (!this.cbxDot.Checked)
                    {
                        flag = buf[0];
                        this.txtReadContent.Text = flag.ToString();
                    }
                    else
                    {
                        if (!this.rdbaleta.Checked)
                        {
                            double val = Math.Round(fbuf[0], 3);
                            this.txtReadContent.Text = val.ToString();
                        }
                        else
                        {
                            byte[] buffer = new byte[4];

                            byte[] kk = BitConverter.GetBytes(buf[0]);

                            byte[] jjj = BitConverter.GetBytes(buf[1]);
                            buffer[3] = jjj[1];
                            buffer[2] = jjj[0];
                            buffer[1] = kk[1];
                            buffer[0] = kk[0];

                            float vvvv = BitConverter.ToSingle(buffer, 0);
                            this.txtReadContent.Text = vvvv.ToString("F1");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("未读取到内容！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取异常：" + ex.ToString());
            }
        }

        private void btnWritePlc_Click(object sender, EventArgs e)
        {
            if (this.btnConectPlc.Text.Trim().Equals("连接plc"))
            {
                MessageBox.Show("未连接plc！");
                return;
            }

            string addr = this.txtWriteAddr.Text.Trim();
            if (string.IsNullOrEmpty(addr))
            {
                MessageBox.Show("写入位置不能为空！");
                this.txtWriteAddr.Focus();
                return;
            }

            bool sucess = false;

            try
            {
                ushort val = Convert.ToUInt16(this.txtWriteContent.Text.Trim());//max=65535
                if (this.rdbs7200.Checked)
                {
                    sucess = _plc_s7200.SetRegister(0x05, addr, val);
                }
                else if (this.rdbsanling.Checked)
                {
                    sucess = _plc_sanling.SetRegisterDB(0x03, "D" + this.txtWriteAddr.Text.Trim(), val);
                }
                else if (this.rdbaleta.Checked)
                {
                    sucess = _AeltaPLC.SetRegister(0x06, "D" + this.txtWriteAddr.Text.Trim(), val);
                }
                if (sucess)
                {
                    MessageBox.Show("写入成功！");
                }
                else
                {
                    MessageBox.Show("写入失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("写入失败：" + ex.ToString());
            }
        }
    }
}
